package com.ylr.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2022-05-31 11:03:25
 * className: CorsConfig
 * version: 1.0
 * description:
 */
@Configuration
public class CorsConfig {

    /**
     * 跨域配置
     */
    @Bean
    public CorsWebFilter corsWebFilter() {
        // 根据url解决跨域问题
        UrlBasedCorsConfigurationSource urlCorsConfig = new UrlBasedCorsConfigurationSource();
        // 跨域基础配置对象
        CorsConfiguration corsConfig = new CorsConfiguration();
        // 允许任何请求通过
        corsConfig.addAllowedHeader("*");
        // 允许任何请求方式通过（GET、POST、DELETE等等）
        corsConfig.addAllowedMethod("*");
        // 允许任何请求来源通过
        corsConfig.addAllowedOrigin("*");
        // 允许请求头中携带Cookie等用户自定义信息
        corsConfig.setAllowCredentials(true);
        // 允许请求最长
        corsConfig.setMaxAge(3600L);
        // 允许所有访问路径
        urlCorsConfig.registerCorsConfiguration("/**", corsConfig);

        return new CorsWebFilter(urlCorsConfig);
    }
}
